import { RequestRoleList } from '@/api/index'
import { ref, type Ref, onMounted } from 'vue'
import type { IRole } from '@/utils/types'

/**
 * 角色管理
 * @returns
 */
export const useRole = () => {
    let roleList: Ref<Array<IRole>> = ref([]) //角色列表
    let disableAddRoleForm: Ref<boolean> = ref(false) //添加角色对话框显示
    let formType = ref<string>('ADD') //ADD 添加 EDIT编辑
    let role = ref<IRole>({ r_id: 0, rolename: '', description: '' })

    onMounted(() => {
        getRoleList()
    })

    const getRoleList = async () => {
        const data = await RequestRoleList()
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            roleList.value = resultInfo.roles
        }
    }

    /**
     * 添加角色
     */
    const bindAddRoleForm = () => {
        formType.value = 'ADD'
        disableAddRoleForm.value = true
    }
    /**
     * 编辑角色
     */
    const bindEditRoleForm = (_role: IRole) => {
        formType.value = 'EDIT'
        role.value = _role
        disableAddRoleForm.value = true
    }
    /**
     * 关闭添加角色对话框
     */
    const closeRoleAddDialog = () => {
        disableAddRoleForm.value = false
        getRoleList()
    }

    return {
        roleList,
        disableAddRoleForm,
        bindAddRoleForm,
        bindEditRoleForm,
        closeRoleAddDialog,
        formType,
        role,
    }
}

export const useRoleEdit = ()=>{
    let roleName = ref<string>('')
    return {roleName}
}

export const useAuth = ()=>{
    const disableAuthMenuDrawer = ref<boolean>(false)
    const roleId = ref<number>(0)
    const bindAuthMenu = (_roleId:number)=>{
        roleId.value = _roleId
        disableAuthMenuDrawer.value = true
    }
    return {disableAuthMenuDrawer,bindAuthMenu,roleId}
}
